package com.ffcs.ffxtc.controller;

import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Component
@ServerEndpoint("/websocket/{ip}/{type}")
public class WebSocketServerPlayBack {

    private static final Map<String, String> clientMap = new ConcurrentHashMap<>();


    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(@PathParam("ip") String ip, @PathParam("type") String type) {
        String key = ip + "&" + type;
        clientMap.put(key, type);
        long count = clientMap.values().stream().filter(type::equals).count();
        // 更新当前用户数量
        System.out.println("当前指定页面人数" + key + "==>数量" + count);
        System.out.println("当前人数总人数" + clientMap.size());
    }


    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(@PathParam("ip") String ip, @PathParam("type") String type) {
        String key = ip + "&" + type;
        clientMap.remove(key, type);
    }
}

